#ifndef _GEN_FIGURE_PDF_HPP_
#define _GEN_FIGURE_PDF_HPP_

#include "figure.hpp"
#include "gen_text_pdf.hpp"
#include <poppler.h>

class figure_pdf:public text_pdf {
	figure* fig;
	cairo_surface_t* image;
	cairo_t* cr;
	cairo_t* rend;
	double image_width, image_height;
public:

	// image -> to png
	// vector -> to pdf
	string convert(string input_file){
		string gf=input_file;
		string fl=(fig->name+"-convert.png");

		// FIXME! generalize!
		// $1=output $2=input
		if(file_type(gf,"eps")) {
			call_script("convert-eps", fl+" "+gf+ " flat '"+fig->background_color+"'");
			fig->garbage.push_back(fl);
		} else if(file_type(gf,"jpg")) {
			call_script("convert-jpg", fl+" "+gf+" flat '"+fig->background_color+"'");
			fig->garbage.push_back(fl);
		} else if(file_type(gf,"png")) {
			call_script("convert-png", fl+" "+gf+" flat '"+fig->background_color+"'");
			fig->garbage.push_back(fl);
		} else {fl=gf;}
		assert(file_type(fl,"pdf")||file_type(fl,"png"), "unsuported figure file tipe", gf);
		assert(file_exist(fl), "failed to convert", gf);
		// read info file if exist
		if(file_exist(fl+".info")){
			xml_config_reader fi((fl+".info").c_str());
			if(fi.Exist("info.size.width"))image_width=fi.GetDouble("info.size.width");
			if(fi.Exist("info.size.height"))image_height=fi.GetDouble("info.size.height");
			fig->garbage.push_back(fl+".info");
		}
		return fl;
	}
	
	void source_pdf(string filename){
		PopplerDocument *document;
	    PopplerPage *page;
    	GError *error=NULL;
    	gchar *absolute,*uri;
    	cairo_status_t status;
    	double w,h;
    	string pdfname(fig->name);
    	pdfname.append("-convert.pdf");
    	fig->garbage.push_back(pdfname);
    	
    	g_type_init();
    	
    	if (g_path_is_absolute(filename.c_str())) { 
        	absolute = g_strdup (filename.c_str());
    	} else {
        	gchar *dir = g_get_current_dir ();
        	absolute = g_build_filename (dir, filename.c_str(), (gchar *) 0);
        	free (dir);
    	}

    	uri=g_filename_to_uri(absolute, NULL, &error); 
    	assert(uri, "failed opening file", filename);
    	document=poppler_document_new_from_file(uri,NULL,&error);
    	assert(document, "failed opening file", filename);
    	int npage= poppler_document_get_n_pages (document);
    	DEBUGINT("number of page", npage);
    	page=poppler_document_get_page(document, 0);
    	assert(page, "failed fetching page", filename);
    	poppler_page_get_size (page, &image_width, &image_height);

    	image = cairo_pdf_surface_create(pdfname.c_str(),image_width,image_height);
    	rend = cairo_create (image);
    	
    	poppler_page_render(page, rend);
    	cairo_restore (rend);
    	g_object_unref(page);
    	g_object_unref(document);
	}
	
	void source_png(string filename){
		image=cairo_image_surface_create_from_png(filename.c_str());
		image_width=(double)cairo_image_surface_get_width(image);
		image_height=(double)cairo_image_surface_get_height(image);
	}

	virtual double draw(void* draw_param){
		fig=dynamic_cast<figure*>(it);
		cr=(cairo_t*)draw_param;
		string gf=convert(fig->input_file);

		
		image=NULL;
		if(file_type(gf,"png")) source_png(gf);
		if(file_type(gf,"pdf")) source_pdf(gf);
		if(!image)return 0.0;

		// FIXME! scale=1 is valid. dont change!

		if(fig->scalex==0.0){
			if(fig->width!=0.0)fig->scalex=get_width()/image_width;
			else fig->scalex=1.0;
		}

		if(fig->scaley==0.0){
			if(fig->height!=0.0)fig->scaley=get_height()/image_height;
			else fig->scaley=1.0;
		}


		set_height(image_height*fig->scaley+get_spacing());
		set_width(image_width*fig->scalex);

		fig->pinning();
		
		cairo_scale(cr, fig->scalex, fig->scaley);
		cairo_set_source_surface(cr, image, get_x()/fig->scalex, get_y()/fig->scaley);
		cairo_paint(cr);
		cairo_scale(cr, 1.0/fig->scalex, 1.0/fig->scaley);
		
		// draw text and add text width to total width
		set_offset(0.0,get_height());
		
		// FIXME! height problem. sync also swf!
		set_height(get_height()+text_pdf::draw(cr)+get_distance());
		return get_height();
	}
};


#endif

